package net.lzzy.practices.aspects;

import net.lzzy.practices.entities.User;
import net.lzzy.practices.services.UserService;
import net.lzzy.practices.utils.AspectUtil;
import net.lzzy.practices.utils.JwtUtil;
import net.lzzy.practices.web.models.ApiResult;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.UUID;

@Aspect
@Component
public class CourseAspect {
    private final UserService service;
@Autowired
    public CourseAspect(UserService service) {
        this.service = service;
    }
//    @Pointcut("execution(* net.lzzy.practoces.web.CourseApiContro)")
//    @Pointcut()
    @Pointcut("execution(* net.lzzy.practices.web.controller.CourseApiController.getCourses(..))||"+
            "execution(* net.lzzy.practices.web.controller.CourseApiController.getOpenCourses(..))||"+
            "execution(* net.lzzy.practices.web.controller.CourseApiController.getPAageCourses(..))||"+
            "execution(* net.lzzy.practices.web.controller.CourseApiController.getPAageOpenCourses(..))"
    )
    private  void coursePoint(){}

    @Pointcut("execution(* net.lzzy.practices.web.controller.CourseApiController.getTeacherCourses(..))||"+
            "execution(* net.lzzy.practices.web.controller.CourseApiController.add(..))||"+
            "execution(* net.lzzy.practices.web.controller.CourseApiController.update(..))||"+
            "execution(* net.lzzy.practices.web.controller.CourseApiController.openClose(..))||"+
            "execution(*  net.lzzy.practices.web.controller.CourseApiController.delete(..))"
    )
    private  void teacherPoint(){

    }

    @Pointcut("execution(* net.lzzy.practices.web.controller.CourseApiController.getStudentCourses(..))")
    private  void student(){

    }
    @Around("coursePoint()")
    public ApiResult aroundCourseMethods(ProceedingJoinPoint point) throws Throwable {
    int [] allowRoles= new int[]{User.TYPE_STUDENT,User.TYPE_TEACHER};
        return getResult(point, allowRoles);


    }

    @Around("teacherPoint()")
    public  ApiResult aroundTeacherMethods(ProceedingJoinPoint point) throws Throwable {
        int [] allowRoles= new int[]{User.TYPE_STUDENT,User.TYPE_TEACHER};
        return getResult(point, allowRoles);
    }
    @Around("teacherPoint()")
    public  ApiResult aroundStudentMethods(ProceedingJoinPoint point) throws Throwable {
        int [] allowRoles= new int[]{User.TYPE_STUDENT,User.TYPE_TEACHER};
        return getResult(point, allowRoles);
    }

    private ApiResult getResult(ProceedingJoinPoint point, int[] allowRoles) throws Throwable {
        String token= AspectUtil.getTokrn(point);
        String uid= JwtUtil.verifyToken(token);
        if(uid==null){
            return new ApiResult(false, "认证信息错误", null);
        }
        User user= service.getById(UUID.fromString(uid));
        if(user == null){
            return new ApiResult(false, "认证信息错误", null);
        }

        if(Arrays.stream(allowRoles).noneMatch(rlo->rlo==user.getType())){
            return new ApiResult(false, "没有权限", null);
        }

        return (ApiResult) point.proceed();
    }


}
